<template>
  <el-card class="box-card"
           v-wechat-title="'消息列表'">
    <div slot="header" class="clearfix">
      <el-form
          ref="queryForm"
          :model="queryParams"
          size="small"
          :inline="true"
          @submit.native.prevent
      >
        <el-form-item>
<!--          <el-button type="primary" icon="el-icon-plus" @click="handleAdd">新增</el-button>-->
          <el-button type="danger" icon="el-icon-delete" :disabled="multiple || isAuth('sys:mq:delete')" @click="handleDelete">删除</el-button>
        </el-form-item>
        <el-form-item>
          <el-input
              v-model="queryParams.title"
              placeholder="推送标题"
              clearable
              style="width: 240px"
              @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" @click="handleReset">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-table
        width="100"
        :data="publishMessageList"
        @selection-change="handleSelectionChange">
      <el-table-column
          type="selection"
          width="55">
      </el-table-column>
      <el-table-column
          label="序号"
          type="index"
          width="50">
      </el-table-column>
      <el-table-column
          align="center"
          prop="blogTitle"
          label="推送标题"
          width="180">
      </el-table-column>
      <el-table-column
          align="center"
          prop="blogId"
          label="博文ID"
          width="180">
      </el-table-column>
      <el-table-column
          prop="createTime"
          label="创建时间"
          width="200">
      </el-table-column>
      <el-table-column align="right" width="180" label="操作">
        <template slot-scope="scope">
          <el-button type="danger"
                     icon="el-icon-delete"
                     circle
                     :disabled="isAuth('sys:mq:delete')"
                     @click="handleDelete(scope.row)"></el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
        v-show="pagination.total>0"
        :limit.sync="pagination.pageSize"
        :page.sync="pagination.currentPage"
        :total="pagination.total"
        :page-sizes="[5, 10, 20, 50]"
        @pagination="handleQuery"
    />
  </el-card>
</template>

<script>
import {getMqMsg, deleteMqMsg} from '@/api/mqMsg'

export default {
  name: "index",
  data() {
    return {
      publishMessageList: [],
      // 非多个禁用
      multiple: true,
      // 选中数组
      ids: [],
      pagination: {
        currentPage: 1,
        pageSize: 10,
        total: 0
      },
      queryParams: {
        currentPage: 1,
        pageSize: 10,
        title: ''
      },
    }
  },
  methods: {
    handleQuery() {
      this.queryParams.currentPage = this.pagination.currentPage
      this.queryParams.pageSize = this.pagination.pageSize
      getMqMsg(this.queryParams).then(res => {
        this.publishMessageList = res.data.records
        this.pagination.total = res.data.total
        this.pagination.currentPage = res.data.current
        this.pagination.pageSize = res.data.size
      });
    },
    handleReset() {
      this.pagination = {
        page: 1,
        limit: 10,
        total: 0
      }
      this.queryParams = {
        clientId: undefined
      }
      this.handleQuery()
    },
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.multiple = !selection.length
    },
    handleDelete(row) {
      const ids = [row.id || this.ids].join(',')
      this.$confirm('确认删除已选中的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteMqMsg(ids).then(() => {
          this.$message.success('删除成功')
          this.handleQuery()
        })
      }).catch(() => {
      })
    },

  },
  created() {
    this.handleQuery()
  }
}
</script>

<style scoped>
.box-card {
  margin: 20px auto;
  max-width: 888px;
}
</style>
